﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Dm.Domain.Entities;
using Dm.Domain.Repository.RepositoryBase;
using System.Data.Entity;
using Dm.Domain.Repository.Factories;
using Dm.Domain.CrossCutting;

namespace Dm.Domain.Repository.EntityRepositories
{
    public class StudentRepository : RepositoryBase<Student>, IStudentRepository
    {
       
        public List<Student> RetrieveAll(bool includeInactive)
        {
            return base._baseContext.Students.Where(s => 
                (includeInactive ? true : s.StatusId == Settings.Statuses.ActiveStatus)).ToList();
        }

        public List<Student> SearchByName(string firstName, string middleName, string lastName)
        {
            return base._baseContext.Students.Where(c =>
                (c.FirstName.Contains(firstName)) &&
                (c.MiddleName.Contains(middleName)) &&
                (c.LastName.Contains(lastName))).ToList();
        }

        public List<Student> SearchAllParameter(string firstName, string middleName, string lastName, string studentNumber, string email, string contactNumber, string address,bool includeInactive = false)
        {
            var result = base._baseContext.Students.Include(c => c.Address).Include(c => c.Status).Where(s =>
                ((string.IsNullOrEmpty(firstName)) ? true : s.FirstName.Contains(firstName)) &&
                ((string.IsNullOrEmpty(middleName)) ? true : s.MiddleName.Contains(middleName)) &&
                ((string.IsNullOrEmpty(lastName)) ? true : s.LastName.Contains(lastName)) &&
                ((string.IsNullOrEmpty(studentNumber)) ? true : s.ControlNumber.Contains(studentNumber)) &&
                ((string.IsNullOrEmpty(email)) ? true : s.Email.Contains(email)) &&
                ((string.IsNullOrEmpty(contactNumber)) ? true : s.ContactNumber.Contains(contactNumber)) &&
                ((string.IsNullOrEmpty(address)) ? true : s.Address.AddressLine1.Contains(address)));
                
                if(includeInactive)
                    return result.ToList();
            return result.Where(s => s.StatusId == Settings.Statuses.ActiveStatus).ToList();
        }

        public Student RetrieveWithDetails(int id, bool includeAddress = false, bool includeStatus = false)
        {
            IQueryable<Student> student = _baseContext.Students.AsQueryable();
            if(includeAddress) student = student.Include(c => c.Address).AsQueryable();
            if(includeStatus) student = student.Include(c => c.Status).AsQueryable();
            return student.FirstOrDefault(c => c.EntityId == id);
        }



        public List<Student> PagedSearch(int page, int rows, string firstName, string middleName, string lastName, string sidx, string sord, out int totalRows, bool includeInactive = false)
        {
            List<Student> student = _baseContext.Students.Where(s => 
                (s.FirstName.Contains(firstName)) &&
                (s.MiddleName.Contains(middleName)) &&
                (s.LastName.Contains(lastName))).ToList();

            totalRows = student.Count();

            if (sord == "desc")
            {
                if(includeInactive == true) return student.OrderByDescending(c => c.ControlNumber).Take(rows).Skip(rows * (page - 1)).ToList();
                else return student.Where(s => s.StatusId == Settings.Statuses.ActiveStatus).OrderByDescending(c => c.ControlNumber).Skip(rows * (page -1)).Take(rows).ToList();
            }
            else
            {
                if (includeInactive == true) return student.OrderBy(c => c.ControlNumber).Take(rows).Skip(rows * (page - 1)).ToList();
                else return student.Where(s => s.StatusId == Settings.Statuses.ActiveStatus).OrderBy(c => c.ControlNumber).Skip(rows * (page -1)).Take(rows).ToList();
            }

           
        }
    }
}
